from optparse import OptionParser
import time

import os, sys
APPLICATION_INSTALL_DIR = os.environ.get('SIMPLEWATCHER_INSTALL')
if not APPLICATION_INSTALL_DIR:
    raise EnvironmentError("must set environment variable SIMPLEWATCHER_INSTALL")
sys.path.append('%s/lib' % APPLICATION_INSTALL_DIR)

from watcher.watch import Watch
from watcher.mail import Mailer

usage = "usage: %prog {-n, --name} watchname {-p, --path} configpath"
parser = OptionParser(usage)
parser.add_option("-n", "--name", dest="name", help="name of the server to start")
parser.add_option("-p", "--path", dest="path", help="configuration path")
(options, args) = parser.parse_args()
name = options.name
configPath = options.path
if not name:
    parser.error("Must have watch name")

w = Watch(name, configPath)
while (True):
    files = w.queue.read(w.queue.config.path.xml_text_content())
    b = ""
    for f in files:
        b += str(f)
    mailer = Mailer(mailFrom="no-reply@localhost.localdomain", mailTo="no-one@localhost.localdomain", subject="tester from watch", body=b)
    mailer.mail()
    time.sleep(30)
